package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.*;
import cc.rengu.igas.momp.common.dao.impl.*;
import cc.rengu.igas.momp.common.entity.*;
import cc.rengu.igas.momp.common.enums.*;
import cc.rengu.igas.momp.common.model.bean.JsonTmpMfdMcInfo;
import cc.rengu.igas.momp.common.util.CommonMessageCheck;
import cc.rengu.igas.momp.common.util.JsonOperation;
import cc.rengu.igas.momp.facade.bean.MchntFeeDiscountMcListInfoBean;
import cc.rengu.igas.momp.facade.request.QueryMchntFeeDiscountMcListInfoRequest;
import cc.rengu.igas.momp.facade.response.QueryMchntFeeDiscountMcListInfoResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户手续费优惠类活动列表查询功能实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/3/16 12:27
 */
public class QueryMchntFeeDiscountMcListService extends RadpService {

    private String globalInstId;
    private String globalQueryType;
    private String globalQueryRole;
    private String globalQueryOrgId;
    private String globalTransDate;
    /**
     * 操作人
     */
    private String globalQueryId;
    /**
     * 当前系统日期
     */
    private String globalTimeStamp;
    /*营销活动查询开始时间*/
    private String globalStartTime;
    /*营销活动查询结束时间*/
    private String globalEndTime;
    private List<TMMcOrgInfo> tmMcOrgInfoList = new ArrayList<>();

    @Override
    protected int bizInit(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizProcess(String s, int i) {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        try {

            QueryMchntFeeDiscountMcListInfoRequest queryMchntFeeDiscountMcListInfoRequest = new QueryMchntFeeDiscountMcListInfoRequest();
            ConvertUtil.convertOutput(queryMchntFeeDiscountMcListInfoRequest);

            QueryMchntFeeDiscountMcListInfoResponse queryMchntFeeDiscountMcListInfoResponse = new QueryMchntFeeDiscountMcListInfoResponse();
            queryMchntFeeDiscountMcListInfoResponse.setHeader(queryMchntFeeDiscountMcListInfoRequest.getHeader());
            BizResponse<QueryMchntFeeDiscountMcListInfoResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(queryMchntFeeDiscountMcListInfoResponse);

            globalInstId = queryMchntFeeDiscountMcListInfoRequest.getHeader().getInstId();
            String timeStamp = new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date());
            String oprId = queryMchntFeeDiscountMcListInfoRequest.getQueryId();
            globalQueryOrgId = queryMchntFeeDiscountMcListInfoRequest.getQueryOrgId();

            rglog.info("机构<{}>在<{}>由<{}>开始查询商户手续费优惠类营销活动列表信息流程", globalInstId, timeStamp, oprId);

            globalQueryType = queryMchntFeeDiscountMcListInfoRequest.getQueryType();
            globalQueryRole = queryMchntFeeDiscountMcListInfoRequest.getQueryRole();
            globalTransDate = queryMchntFeeDiscountMcListInfoRequest.getHeader().getTransDate();
            globalStartTime = queryMchntFeeDiscountMcListInfoRequest.getStartTime();
            globalEndTime = queryMchntFeeDiscountMcListInfoRequest.getEndTime();
            /* 报文检查 */
            CommonMessageCheck.checkBizMessageHeader(globalQueryType, oprId, globalQueryRole, rglog);

            /* 检查操作员所属机构号 */
            if (StringUtil.isNullorEmpty(globalQueryOrgId)) {
                rglog.error("法人机构号<{}>的操作员<{}>的所属机构为空!", globalInstId, oprId);
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            //查询上送机构及下属机构
            McOrgInfoMapper mcOrgInfoMapper = new McOrgInfoMapperImpl();
            tmMcOrgInfoList = mcOrgInfoMapper.selectAllMcOrgInfoListByinstIdOrgId(queryMchntFeeDiscountMcListInfoRequest.getHeader().getInstId(), globalQueryOrgId);
            if (null == tmMcOrgInfoList || tmMcOrgInfoList.size() == 0) {
                /* 代理商、收银员或用户 */
                rglog.error("查询当前角色<{}>机构<{}>及其下属机构失败!", queryMchntFeeDiscountMcListInfoRequest.getQueryRole(), globalQueryOrgId);
                throw new BizException(RespCodeEnum.GET_MC_ORG_INFO_ERROR.getRespCode(), RespCodeEnum.GET_MC_ORG_INFO_ERROR.getRespDesc());
            }
            // 服务调用
            BizResponse<QueryMchntFeeDiscountMcListInfoResponse> bizResponseNew = queryMchntFeeDiscountMcListInfo(xmlTreeUtil, queryMchntFeeDiscountMcListInfoRequest, queryMchntFeeDiscountMcListInfoResponse);

            ConvertUtil.convertInput(bizResponseNew.getResult());

        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e));
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 查询商户手续费优惠营销活动列表查询接口应答对象营销活动列表信息
     *
     * @param xmlTreeUtil                             内部XML树
     * @param queryMchntFeeDiscountMcListInfoRequest  商户手续费优惠营销活动列表查询接口请求对象
     * @param queryMchntFeeDiscountMcListInfoResponse 商户手续费优惠营销活动列表查询接口应答对象
     * @return cc.rengu.oltp.service.model.BizResponse<cc.rengu.igas.momp.facade.response.QueryMchntFeeDiscountMcListInfoResponse> 商户手续费优惠营销活动列表查询接口应答对象
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/29 22:16
     */
    private BizResponse<QueryMchntFeeDiscountMcListInfoResponse> queryMchntFeeDiscountMcListInfo(XmlTreeUtil xmlTreeUtil, QueryMchntFeeDiscountMcListInfoRequest queryMchntFeeDiscountMcListInfoRequest, QueryMchntFeeDiscountMcListInfoResponse queryMchntFeeDiscountMcListInfoResponse)
            throws Exception {

        BizResponse<QueryMchntFeeDiscountMcListInfoResponse> bizResponse = new BizResponse<>();
        List<MchntFeeDiscountMcListInfoBean> mchntFeeDiscountMcListInfoBeanList;

        List<MfdMcListInfoOutObj> mfdMcListInfoOutObjList = new ArrayList<>();
        PageResult pageResult;

        /* 数据库入参赋值 */
        MfdMcListInfoInObj mfdMcListInfoInObj = setupDatabaseInputObject(queryMchntFeeDiscountMcListInfoRequest);

        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(Integer.parseInt(queryMchntFeeDiscountMcListInfoRequest.getPageIndex()));
        pageInfo.setPageSize(Integer.parseInt(queryMchntFeeDiscountMcListInfoRequest.getPageSize()));

        rglog.debug("当前是<{}>查询数据", QueryRoleEnum.getByValue(globalQueryRole).getQueryRoleDesc());
        if (QueryTypeEnum.BANK_QUERY_TYPE.getQueryTypeCode().equals(globalQueryType) && QueryRoleEnum.BANK_AUDIT_QYERY_ROLE.getQueryRoleCode().equals(globalQueryRole)) {
            //银行审批岗因为数据查询的为业务数据临时表所以单独处理
            mchntFeeDiscountMcListInfoBeanList = processTmpApsMcListInfo(queryMchntFeeDiscountMcListInfoRequest);

            if (null == mchntFeeDiscountMcListInfoBeanList || mchntFeeDiscountMcListInfoBeanList.isEmpty()) {
                rglog.info("根据查询条件查询到0条手续费减免类营销活动信息!");
                queryMchntFeeDiscountMcListInfoResponse.setTotalPage(CommonConstant.ZERO_COMMON_CONSTANT);
                queryMchntFeeDiscountMcListInfoResponse.setTotalRows(CommonConstant.ZERO_COMMON_CONSTANT);
            } else {
                queryMchntFeeDiscountMcListInfoResponse.setTotalRows(String.valueOf(mchntFeeDiscountMcListInfoBeanList.size()));
                if (CommonConstant.ZERO == (mchntFeeDiscountMcListInfoBeanList.size() % Integer.parseInt(queryMchntFeeDiscountMcListInfoRequest.getPageSize()))) {
                    queryMchntFeeDiscountMcListInfoResponse.setTotalPage(String.valueOf(mchntFeeDiscountMcListInfoBeanList.size() / Math.abs(Integer.parseInt(queryMchntFeeDiscountMcListInfoRequest.getPageSize()))));
                } else {
                    queryMchntFeeDiscountMcListInfoResponse.setTotalPage(String.valueOf(mchntFeeDiscountMcListInfoBeanList.size() / Math.abs(Integer.parseInt(queryMchntFeeDiscountMcListInfoRequest.getPageSize())) + 1));
                }

                /* 分页查询 */
                int fromIndex = Integer.parseInt(queryMchntFeeDiscountMcListInfoRequest.getPageIndex()) * Integer.parseInt(queryMchntFeeDiscountMcListInfoRequest.getPageSize());
                int endIndex = fromIndex + Integer.parseInt(queryMchntFeeDiscountMcListInfoRequest.getPageSize());
                if (fromIndex <= mchntFeeDiscountMcListInfoBeanList.size()) {
                    List<MchntFeeDiscountMcListInfoBean> mchntFeeDiscountMcListInfoBeanList1 = mchntFeeDiscountMcListInfoBeanList.subList(fromIndex, Math.min(endIndex, mchntFeeDiscountMcListInfoBeanList.size()));
                    queryMchntFeeDiscountMcListInfoResponse.setMcListInfo(mchntFeeDiscountMcListInfoBeanList1);
                }
            }
        } else {
            /* 根据角色权限查询数据,仅由银行人员可查数据 */
            if (QueryTypeEnum.BANK_QUERY_TYPE.getQueryTypeCode().equals(globalQueryType)) {
                /* 银行人员,需要拼接营销活动机构信息表查询 */

                /* 机构编号 */
                if (!StringUtil.isNullorEmpty(globalQueryOrgId)) {
                    mfdMcListInfoInObj.setOrgId(globalQueryOrgId);
                    rglog.info("ORG_ID=<{}>", globalQueryOrgId);
                }

                /* 临时表数据字段1模糊查询字符串拼接 */
                if (QueryRoleEnum.BANK_AUDIT_QYERY_ROLE.getQueryRoleCode().equals(globalQueryRole)) {
                    String bizData = "{\"inst_id\":\"" + globalInstId + "\"%";
                    mfdMcListInfoInObj.setBizData1(bizData);
                }
                if (!StringUtil.isNullorEmpty(globalStartTime)) {
                    mfdMcListInfoInObj.setStartTime(globalStartTime);
                }
                if (!StringUtil.isNullorEmpty(globalEndTime)) {
                    mfdMcListInfoInObj.setEndTime(globalEndTime);
                }
                pageResult = queryMchntFeeDiscountMcListInfoByBank(mfdMcListInfoInObj, pageInfo);
                if (null == pageResult || null == pageResult.getPageInfo() || null == pageResult.getResult()) {
                    rglog.info("根据查询条件查询到0条手续费减免类营销活动信息!");
                    queryMchntFeeDiscountMcListInfoResponse.setTotalPage(CommonConstant.ZERO_COMMON_CONSTANT);
                    queryMchntFeeDiscountMcListInfoResponse.setTotalRows(CommonConstant.ZERO_COMMON_CONSTANT);
                }
            } else {
                /* 其他查询角色不支持 */
                rglog.error("当前<{}>角色的查询用户<{}>无权限操作数据!", globalQueryRole, globalQueryType);
                throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
            }

            /* 根据取出记录条数做对应处理 */
            if (null == pageResult || null == pageResult.getPageInfo() || null == pageResult.getResult() || pageResult.getResult().isEmpty()) {
                rglog.info("根据查询条件查询到0条手续费减免类营销活动信息!");

                queryMchntFeeDiscountMcListInfoResponse.setTotalPage(CommonConstant.ZERO_COMMON_CONSTANT);
                queryMchntFeeDiscountMcListInfoResponse.setTotalRows(CommonConstant.ZERO_COMMON_CONSTANT);

            } else {
                MfdMcListInfoOutObj MfdMcListInfoOutObj;
                for (Object object : pageResult.getResult()) {
                    MfdMcListInfoOutObj = (MfdMcListInfoOutObj) object;
                    //循环计算已经消耗的额度并赋值给返回的对象
                    String alredyUsed = calMchntFeeDiscountAlredyUsedFee(MfdMcListInfoOutObj.getMcNo(), MfdMcListInfoOutObj.getMcStartDate(), MfdMcListInfoOutObj.getMcEndDate(), MfdMcListInfoOutObj.getMcStatus());
                    MfdMcListInfoOutObj.setTotalAmtAcc(alredyUsed);
                    mfdMcListInfoOutObjList.add(MfdMcListInfoOutObj);
                }

                rglog.info("SIZE=<{}>", pageResult.getResult().size());
                List<MchntFeeDiscountMcListInfoBean> mchntFeeDiscountMcListInfoBeanList1 = new ArrayList<>();
                mchntFeeDiscountMcListInfoBeanList = mfdMcListInfoOutObjList.stream().map(this::convertMchntFeeDiscountMcListInfo).collect(Collectors.toList());
                queryMchntFeeDiscountMcListInfoResponse.setMcListInfo(mchntFeeDiscountMcListInfoBeanList);
                //判断上送的商户号是否为空
                if (!StringUtil.isNullorEmpty(queryMchntFeeDiscountMcListInfoRequest.getMchntNo())) {
                    McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
                    //取营销活动商户表
                    List<TMMcMchntInfo> tmMcMchntInfoList = mcMchntInfoMapper.selectMfdMchntInfoListByinstIdMchntNo(globalInstId, queryMchntFeeDiscountMcListInfoRequest.getMchntNo());
                    if (null != tmMcMchntInfoList && tmMcMchntInfoList.size() > 0) {
                        //取上送的商户参与的营销活动列表
                        for (TMMcMchntInfo tmMcMchntInfo : tmMcMchntInfoList) {
                            List<MchntFeeDiscountMcListInfoBean> mchntFeeDiscountMcListInfoBeanList2 = mchntFeeDiscountMcListInfoBeanList;
                            mchntFeeDiscountMcListInfoBeanList2 = mchntFeeDiscountMcListInfoBeanList2.stream().filter(item -> tmMcMchntInfo.getMcNo().equals(item.getMcNo())).collect(Collectors.toList());
                            if (null != mchntFeeDiscountMcListInfoBeanList2 && mchntFeeDiscountMcListInfoBeanList2.size() > 0) {
                                mchntFeeDiscountMcListInfoBeanList1.addAll(mchntFeeDiscountMcListInfoBeanList2);
                            }
                        }
                    }
                    queryMchntFeeDiscountMcListInfoResponse.setMcListInfo(mchntFeeDiscountMcListInfoBeanList1);
                }
                queryMchntFeeDiscountMcListInfoResponse.setTotalPage(Integer.toString(pageResult.getPageInfo().getTotalPage()));
                queryMchntFeeDiscountMcListInfoResponse.setTotalRows(Integer.toString(pageResult.getPageInfo().getTotalRows()));

            }
        }

        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        queryMchntFeeDiscountMcListInfoResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        queryMchntFeeDiscountMcListInfoResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        queryMchntFeeDiscountMcListInfoResponse.setPageIndex(queryMchntFeeDiscountMcListInfoRequest.getPageIndex());
        queryMchntFeeDiscountMcListInfoResponse.setPageSize(queryMchntFeeDiscountMcListInfoRequest.getPageSize());

        bizResponse.setResult(queryMchntFeeDiscountMcListInfoResponse);

        return bizResponse;
    }

    /**
     * 银行人员查询数据
     *
     * @param mfdMcListInfoInObj 商户手续费优惠营销活动列表查询接口应答对象营销活动列表查询使用实体类入参
     * @param pageInfo           数据库操作分页请求实体类
     * @return cc.rengu.oltp.utility.bean.PageResult 数据库操作分页应答实体类
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/27 23:14
     */
    private PageResult queryMchntFeeDiscountMcListInfoByBank(MfdMcListInfoInObj mfdMcListInfoInObj, PageInfo pageInfo) throws Exception {

        switch (QueryRoleEnum.getByValue(globalQueryRole)) {
            case BANK_BASIC_QUERY_ROLE:
                /* 银行录入岗 */
                return selectMfdMcListInfoByBankBasicRole(mfdMcListInfoInObj, pageInfo);
            case BANK_PUBLISH_QYERY_ROLE:
                /* 银行发布岗 */
                return selectMfdMcListInfoByBankPublishRole(mfdMcListInfoInObj, pageInfo);
            default:
                /* 其他查询角色不支持 */
                rglog.error("当前<{}>角色的查询用户<{}>无权限操作数据!", globalQueryRole, globalQueryType);
                throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
        }
    }

    /**
     * 银行操作员查询减免类营销活动列表数据库操作
     *
     * @param mfdMcListInfoInObj 商户手续费优惠营销活动列表查询接口应答对象营销活动列表查询使用实体类入参
     * @param pageInfo           数据库操作分页请求实体类
     * @return cc.rengu.oltp.utility.bean.PageResult 数据库操作分页应答实体类
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 00:47
     */
    private PageResult selectMfdMcListInfoByBankBasicRole(MfdMcListInfoInObj mfdMcListInfoInObj, PageInfo pageInfo) throws Exception {

        McInfoListMapper mcInfoListMapper = new McInfoListMapperImpl();
        return mcInfoListMapper.selectMfdMcListInfoByBankBasicRole(mfdMcListInfoInObj, pageInfo);
    }

    /**
     * 银行发布员查询减免类营销活动列表数据库操作
     *
     * @param mfdMcListInfoInObj 商户手续费优惠营销活动列表查询接口应答对象营销活动列表查询使用实体类入参
     * @param pageInfo           数据库操作分页请求实体类
     * @return cc.rengu.oltp.utility.bean.PageResult 数据库操作分页应答实体类
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 01:13
     */
    private PageResult selectMfdMcListInfoByBankPublishRole(MfdMcListInfoInObj mfdMcListInfoInObj, PageInfo pageInfo) throws Exception {

        McInfoListMapper mcInfoListMapper = new McInfoListMapperImpl();
        return mcInfoListMapper.selectMfdMcListInfoByBankPublishRole(mfdMcListInfoInObj, pageInfo);
    }

    /**
     * 数据库实体类和接口实体类数据转换
     *
     * @param mfdMcListInfoOutObj 营销活动列表查询使用实体类出参
     * @return cc.rengu.igas.momp.facade.bean.MchntFeeDiscountMcListInfoBean 减免类营销活动列表查询接口应答对象
     * @author Jinan Liu
     * @date 2020/5/28 00:15
     */
    private MchntFeeDiscountMcListInfoBean convertMchntFeeDiscountMcListInfo(MfdMcListInfoOutObj mfdMcListInfoOutObj) {

        MchntFeeDiscountMcListInfoBean mchntFeeDiscountMcListInfoBean = new MchntFeeDiscountMcListInfoBean();
        try {
            /* 营销活动编号 */
            mchntFeeDiscountMcListInfoBean.setMcNo(mfdMcListInfoOutObj.getMcNo());
            /* 营销活动名称 */
            mchntFeeDiscountMcListInfoBean.setMcName(mfdMcListInfoOutObj.getMcName());
            /* 营销活动类型 */
            mchntFeeDiscountMcListInfoBean.setMcType(mfdMcListInfoOutObj.getMcType());
            /* 营销活动状态 */
            mchntFeeDiscountMcListInfoBean.setMcStatus(mfdMcListInfoOutObj.getMcStatus());
            /* 活动起始日期 */
            mchntFeeDiscountMcListInfoBean.setMcStartDate(mfdMcListInfoOutObj.getMcStartDate());
            /* 活动截止日期 */
            mchntFeeDiscountMcListInfoBean.setMcEndDate(mfdMcListInfoOutObj.getMcEndDate());
            /* 活动结束日期 */
            mchntFeeDiscountMcListInfoBean.setMcActualEndDate(mfdMcListInfoOutObj.getMcActualEndDate());
            /* 活动预算 */
            mchntFeeDiscountMcListInfoBean.setBudget(mfdMcListInfoOutObj.getBudget());
            /* 已消耗预算 */
            if (StringUtil.isNullorEmpty(mfdMcListInfoOutObj.getTotalAmtAcc())) {
                mchntFeeDiscountMcListInfoBean.setBudgetUsed("0.00");
            } else {
                mchntFeeDiscountMcListInfoBean.setBudgetUsed(mfdMcListInfoOutObj.getTotalAmtAcc());
            }
            /* 审批状态 */
            mchntFeeDiscountMcListInfoBean.setAuditStatus(mfdMcListInfoOutObj.getAuditStatus());
            /* 创建人 */
            mchntFeeDiscountMcListInfoBean.setOprId(mfdMcListInfoOutObj.getOprId());
            /* 创建时间 */
            mchntFeeDiscountMcListInfoBean.setCreateTime(mfdMcListInfoOutObj.getCreateTime());
            /* 最后修改人 */
            if (StringUtil.isNullorEmpty(mfdMcListInfoOutObj.getLastOprId())) {
                mchntFeeDiscountMcListInfoBean.setLastOprId("");
            } else {
                mchntFeeDiscountMcListInfoBean.setLastOprId(mfdMcListInfoOutObj.getLastOprId());
            }
            /* 修改时间 */
            if (StringUtil.isNullorEmpty(mfdMcListInfoOutObj.getUpdateTime())) {
                mchntFeeDiscountMcListInfoBean.setUpdateTime("");
            } else {
                mchntFeeDiscountMcListInfoBean.setUpdateTime(mfdMcListInfoOutObj.getUpdateTime());
            }
            //判断上送机构及其下属机构是否包含活动原创建机构
            List<TMMcOrgInfo> newList = tmMcOrgInfoList.stream().filter(item -> item.getOrgId().equals(mfdMcListInfoOutObj.getCrtOrgId())).collect(Collectors.toList());
            if (null != newList && newList.size() > 0) {
                /* 已参与营销活动 */
                mchntFeeDiscountMcListInfoBean.setAttendFlag(CommonConstant.ATTEND_FLAG_YES);
            } else {
                /* 未参与营销活动 */
                mchntFeeDiscountMcListInfoBean.setAttendFlag(CommonConstant.ATTEND_FLAG_NO);
            }

        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("转换营销活动信息异常,异常信息:<{}>", byteArrayOutputStream.toString());
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
        return mchntFeeDiscountMcListInfoBean;
    }

    /**
     * 数据库查询前赋值数据库实体类入参
     *
     * @param queryMchntFeeDiscountMcListInfoRequest 商户手续费优惠营销活动列表查询接口请求对象
     * @return cc.rengu.igas.momp.common.entity.CouponMcListInfoInObj 商户手续费优惠营销活动列表查询接口应答对象营销活动列表查询使用实体类入参
     * @author Jinan Liu
     * @date 2020/5/28 12:57
     */
    private MfdMcListInfoInObj setupDatabaseInputObject(QueryMchntFeeDiscountMcListInfoRequest queryMchntFeeDiscountMcListInfoRequest) {

        MfdMcListInfoInObj mfdMcListInfoInObj = new MfdMcListInfoInObj();

        /* 机构编码 */
        mfdMcListInfoInObj.setInstId(globalInstId);
        rglog.info("INST_ID=<{}>", globalInstId);

        /* 营销活动编号 */
        if (!StringUtil.isNullorEmpty(queryMchntFeeDiscountMcListInfoRequest.getMcNo())) {
            mfdMcListInfoInObj.setMcNo(queryMchntFeeDiscountMcListInfoRequest.getMcNo());
        }
        /* 营销活动名称 */
        if (!StringUtil.isNullorEmpty(queryMchntFeeDiscountMcListInfoRequest.getMcName())) {
            mfdMcListInfoInObj.setMcName(queryMchntFeeDiscountMcListInfoRequest.getMcName());
        }
        /* 营销活动类型 */
        if (!StringUtil.isNullorEmpty(queryMchntFeeDiscountMcListInfoRequest.getMcType())) {
            mfdMcListInfoInObj.setMcType(queryMchntFeeDiscountMcListInfoRequest.getMcType());
        }
        /* 营销活动状态 */
        if (!StringUtil.isNullorEmpty(queryMchntFeeDiscountMcListInfoRequest.getMcStatus())) {
            mfdMcListInfoInObj.setMcStatus(queryMchntFeeDiscountMcListInfoRequest.getMcStatus());
            if (queryMchntFeeDiscountMcListInfoRequest.getMcStatus().equals(McStatusEnum.ALL_ENDED.getMcStatusCode())) {
                mfdMcListInfoInObj.setRemark1(McStatusEnum.ALL_ENDED.getMcStatusCode());
            }
        }
        /* 审批状态 */
        if (!StringUtil.isNullorEmpty(queryMchntFeeDiscountMcListInfoRequest.getAuditStatus())) {
            mfdMcListInfoInObj.setAuditStatus(queryMchntFeeDiscountMcListInfoRequest.getAuditStatus());
        }
        /* 当前系统日期 */
        if (!StringUtil.isNullorEmpty(queryMchntFeeDiscountMcListInfoRequest.getHeader().getTransDate())) {
            mfdMcListInfoInObj.setTxnDate(queryMchntFeeDiscountMcListInfoRequest.getHeader().getTransDate());
            rglog.info("TXN_DATE=<{}>", queryMchntFeeDiscountMcListInfoRequest.getHeader().getTransDate());
        }

        return mfdMcListInfoInObj;
    }

    /**
     * 银行审批岗数据查询
     *
     * @param queryMchntFeeDiscountMcListInfoRequest 手续费减免活动列表查询接口请求对象
     * @return java.util.List<cc.rengu.igas.momp.facade.bean.MchntFeeDiscountMcListInfoBean> 手续费减免类营销活动列表查询接口应答对象
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/6/20 16:53
     */
    private List<MchntFeeDiscountMcListInfoBean> processTmpApsMcListInfo(QueryMchntFeeDiscountMcListInfoRequest queryMchntFeeDiscountMcListInfoRequest) throws Exception {

        List<MchntFeeDiscountMcListInfoBean> mchntFeeDiscountMcListInfoBeanList;

        TMPlatBizTmp tmPlatBizTmp = new TMPlatBizTmp();
        tmPlatBizTmp.setInstId(globalInstId);
        tmPlatBizTmp.setOrgId(globalQueryOrgId);
        tmPlatBizTmp.setOprTableEname(TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode());

        //营销活动编号
        if (!StringUtil.isNullorEmpty(queryMchntFeeDiscountMcListInfoRequest.getMcNo())) {
            tmPlatBizTmp.setMcNo(queryMchntFeeDiscountMcListInfoRequest.getMcNo());
        }
        //营销活动状态
        if (!StringUtil.isNullorEmpty(queryMchntFeeDiscountMcListInfoRequest.getMcStatus())) {
            tmPlatBizTmp.setMcStatus(queryMchntFeeDiscountMcListInfoRequest.getMcStatus());
        }

        // 查询临时表获取数据
        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        List<TMPlatBizTmp> tmPlatBizTmpList = platBizTmpMapper.selectTmpMcListInfo(tmPlatBizTmp);
        if (null != tmPlatBizTmpList && !tmPlatBizTmpList.isEmpty()) {
            mchntFeeDiscountMcListInfoBeanList = tmPlatBizTmpList.stream().map(this::convertTmpMcInfo).collect(Collectors.toList());
            rglog.debug("活动查询结果 SIZE=<{}>", mchntFeeDiscountMcListInfoBeanList.size());

            /* 代码过滤查询条件 */
            //按照营销活动名称筛选
            if (!StringUtil.isNullorEmpty(queryMchntFeeDiscountMcListInfoRequest.getMcName())) {
                mchntFeeDiscountMcListInfoBeanList = mchntFeeDiscountMcListInfoBeanList.stream().filter(item -> item.getMcName().contains(queryMchntFeeDiscountMcListInfoRequest.getMcName())).collect(Collectors.toList());
            }
            //按照营销活动类型筛选
            if (!StringUtil.isNullorEmpty(queryMchntFeeDiscountMcListInfoRequest.getMcType())) {
                mchntFeeDiscountMcListInfoBeanList = mchntFeeDiscountMcListInfoBeanList.stream().filter(item -> item.getMcType().equals(queryMchntFeeDiscountMcListInfoRequest.getMcType())).collect(Collectors.toList());
            }
            return mchntFeeDiscountMcListInfoBeanList;
        } else {
            return null;
        }
    }

    /**
     * 转换换业务数据临时表临时数据
     *
     * @param tmPlatBizTmp 业务数据临时表
     * @return cc.rengu.igas.momp.facade.bean.MchntFeeDiscountMcListInfoBean 手续费减免类营销活动列表查询接口应答对象
     * @author Jinan Liu
     * @date 2020/6/20 15:37
     */
    private MchntFeeDiscountMcListInfoBean convertTmpMcInfo(TMPlatBizTmp tmPlatBizTmp) {

        MchntFeeDiscountMcListInfoBean mchntFeeDiscountMcListInfoBean = new MchntFeeDiscountMcListInfoBean();
        try {
            String mcNo;
            String mcName;
            String mcType;
            String mcStartDate;
            String mcEndDate;
            String mcActualEndDate;
            String budget;
            /* 根据营销活动类型解析BIZ_DATA1字段内容 */
            String mcTypeOne = tmPlatBizTmp.getMcNo().substring(2, 3);
            if (McTypeEnum.MC_TYPE_MFD.getMcTypeCode().equals(mcTypeOne)) {
                JsonTmpMfdMcInfo jsonTmpMfdMcInfo = JsonOperation.parseMfdMcInfoTmpBizData1(tmPlatBizTmp, rglog);
                mcNo = jsonTmpMfdMcInfo.getMcNo();
                mcName = jsonTmpMfdMcInfo.getMcName();
                mcType = jsonTmpMfdMcInfo.getMcType();
                mcStartDate = jsonTmpMfdMcInfo.getMcStartDate();
                mcEndDate = jsonTmpMfdMcInfo.getMcEndDate();
                mcActualEndDate = jsonTmpMfdMcInfo.getMcActualEndDate();
                budget = jsonTmpMfdMcInfo.getBudget();
            } else {
                rglog.error("不支持的营销活动类型<{}>,不可转换数据!", mcTypeOne);
                throw new BizException(RespCodeEnum.NOT_SUPPORTED_MC_TYPE_ERROR.getRespCode(), RespCodeEnum.NOT_SUPPORTED_MC_TYPE_ERROR.getRespDesc());
            }

            /* 营销活动编号 */
            mchntFeeDiscountMcListInfoBean.setMcNo(mcNo);
            /* 营销活动名称 */
            mchntFeeDiscountMcListInfoBean.setMcName(mcName);
            /* 营销活动类型 */
            mchntFeeDiscountMcListInfoBean.setMcType(mcType);
            /* 活动起始日期 */
            mchntFeeDiscountMcListInfoBean.setMcStartDate(mcStartDate);
            /* 活动截止日期 */
            mchntFeeDiscountMcListInfoBean.setMcEndDate(mcEndDate);
            /* 活动结束日期 */
            mchntFeeDiscountMcListInfoBean.setMcActualEndDate(mcActualEndDate);
            /* 活动预算 */
            mchntFeeDiscountMcListInfoBean.setBudget(budget);
            /* 已消耗预算 */
            mchntFeeDiscountMcListInfoBean.setBudgetUsed(CommonConstant.ZERO_AMOUNT);
            /* 审批状态 */
            mchntFeeDiscountMcListInfoBean.setAuditStatus(tmPlatBizTmp.getAuditStatus());
            /* 创建人 */
            mchntFeeDiscountMcListInfoBean.setOprId(tmPlatBizTmp.getOprId());
            /* 创建时间 */
            mchntFeeDiscountMcListInfoBean.setCreateTime(tmPlatBizTmp.getCreateTime());
            /* 最后修改人 */
            mchntFeeDiscountMcListInfoBean.setLastOprId(tmPlatBizTmp.getLastOprId());
            /* 修改时间 */
            mchntFeeDiscountMcListInfoBean.setUpdateTime(tmPlatBizTmp.getLastUpdateTime());

        } catch (Exception e) {
            rglog.error("转换营销活动临时表信息异常,异常信息:<{}>", StringUtil.ExceptionToString(e));
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
        return mchntFeeDiscountMcListInfoBean;
    }

    /**
     * 请求报文体内容检查
     *
     * @param queryMchntFeeDiscountMcListInfoRequest 手续费减免类活动列表查询接口请求对象
     * @author Jinan Liu
     * @date 2020/6/20 09:55
     */
    private void messageValidation(QueryMchntFeeDiscountMcListInfoRequest queryMchntFeeDiscountMcListInfoRequest) {

        if (StringUtil.isNullorEmpty(queryMchntFeeDiscountMcListInfoRequest.getHeader().getTransDate())) {
            rglog.error("交易日期必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalTransDate = queryMchntFeeDiscountMcListInfoRequest.getHeader().getTransDate();
        }

        if (StringUtil.isNullorEmpty(queryMchntFeeDiscountMcListInfoRequest.getHeader().getTransTime())) {
            rglog.error("交易时间必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(queryMchntFeeDiscountMcListInfoRequest.getHeader().getInstId())) {
            rglog.error("法人机构号必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalInstId = queryMchntFeeDiscountMcListInfoRequest.getHeader().getInstId();
        }

        if (StringUtil.isNullorEmpty(queryMchntFeeDiscountMcListInfoRequest.getHeader().getTraceNo())) {
            rglog.error("流水号必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(queryMchntFeeDiscountMcListInfoRequest.getQueryType())) {
            rglog.error("操作员类型为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryType = queryMchntFeeDiscountMcListInfoRequest.getQueryType();
        }

        if (StringUtil.isNullorEmpty(queryMchntFeeDiscountMcListInfoRequest.getQueryId())) {
            rglog.error("操作员ID为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryId = queryMchntFeeDiscountMcListInfoRequest.getQueryId();
        }

        if (StringUtil.isNullorEmpty(queryMchntFeeDiscountMcListInfoRequest.getQueryRole())) {
            rglog.error("操作员角色为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryRole = queryMchntFeeDiscountMcListInfoRequest.getQueryRole();
        }

        if (StringUtil.isNullorEmpty(queryMchntFeeDiscountMcListInfoRequest.getQueryOrgId())) {
            rglog.error("操作员所属法人机构为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryOrgId = queryMchntFeeDiscountMcListInfoRequest.getQueryOrgId();
        }

        if (StringUtil.isNullorEmpty(queryMchntFeeDiscountMcListInfoRequest.getPageIndex())) {
            rglog.error("当前页数为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            int pageIndex = Integer.parseInt(queryMchntFeeDiscountMcListInfoRequest.getPageIndex());
            if (CommonConstant.ZERO > pageIndex) {
                rglog.error("当前查询页数必须大于0! PAGE_INDEX=<{}>", pageIndex);
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
        }

        if (StringUtil.isNullorEmpty(queryMchntFeeDiscountMcListInfoRequest.getPageSize())) {
            rglog.error("页显示记录数为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            int pageSize = Integer.parseInt(queryMchntFeeDiscountMcListInfoRequest.getPageSize());
            if (CommonConstant.ONE > pageSize) {
                rglog.error("每页记录数必须大于等于1! PAGE_SIZE=<{}>", pageSize);
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
        }
        globalTimeStamp = new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date());
    }

    /**
     * 查询手续费优惠活动已经使用的优惠额度值
     *
     * @throws Exception
     * @author liujinan
     * @date 2020/9/16
     */
    private String calMchntFeeDiscountAlredyUsedFee(String mcNo, String mcStartDate, String mcEndDate, String mcStatus) throws Exception {
        if (McStatusEnum.DRAFT.getMcStatusCode().equals(mcStatus) || McStatusEnum.NOT_PUBLISHED.getMcStatusCode().equals(mcStatus)) {
            rglog.info("活动是草稿或未发布状态，已使用预算直接返回0.00");
            String alredyUse = CommonConstant.ZERO_AMOUNT;
            return alredyUse;
        }
        int batchStatus = CommonConstant.FLAG_NOT_SET;
        String lastDate = DateUtil.getBaseDateOffsetDay(globalTransDate, CommonConstant.MINUS_ONE);
        if (DateUtil.compareDate(lastDate, mcEndDate) > 0) {
            lastDate = mcEndDate;
        }
        String lastSecondDate = DateUtil.getBaseDateOffsetDay(lastDate, CommonConstant.MINUS_ONE);

        /* 查询商户手续费优惠营销活动统计表数据  规则为0的数据*/
        //查询统计信息备份表批次信息 查询营销活动信息表的批量处理情况即可
        String batchDate;
        String statPrimaryKey = "Fee" + globalInstId + "_" + mcNo;
        TMStatBak tmStatBak = selectTMStatBakByStatPrimaryKey(globalInstId, McTypeEnum.MCHNT_FEE_DISCOUNT.getMcTypeCode(), statPrimaryKey);
        //批次表为空，从来没执行过批量任务
        if (null == tmStatBak) {
            //从来没执行过批量任务，则从活动开始的第一天开始取数据
            batchDate = mcStartDate;
        } else {
            batchDate = DateUtil.getBaseDateOffsetDay(tmStatBak.getBatchDate(), 1);
        }
        /* 查询商户手续费优惠营销活动统计表数据  规则为0的数据*/
        TMStatMfd tmStatMfd = selectMchntFeeDiscountStatisticsByPK(globalInstId, mcNo, CommonConstant.ZERO_COMMON_CONSTANT);
        if (null == tmStatMfd) {
            rglog.info("机构<{}>的营销活动<{}>的规则<{}>商户手续费优惠营销活动统计表数据查询失败!", globalInstId, mcNo, CommonConstant.ZERO_COMMON_CONSTANT);

            /* 取到0条数据,说明该营销活动还未产生统计值,按所有统计值都是0进行处理 */
            tmStatMfd = new TMStatMfd();
            tmStatMfd.setTotalQtyAcc(CommonConstant.ZERO_COMMON_CONSTANT);
            tmStatMfd.setTotalAmtAcc(CommonConstant.ZERO_COMMON_CONSTANT);
            tmStatMfd.setTxnQtyAcc(CommonConstant.ZERO_COMMON_CONSTANT);
            tmStatMfd.setTxnAmtAcc(CommonConstant.ZERO_COMMON_CONSTANT);

        }
        /* 查询商户手续费优惠营销活动单日统计表数据 规则为0的数据*/
        TMStatMfdDay tmStatMfdDay = selectMchntFeeDiscountDayStatisticsByPK(globalInstId, mcNo, CommonConstant.ZERO_COMMON_CONSTANT, globalTransDate);
        if (null == tmStatMfdDay) {
            rglog.error("机构<{}>的商户手续费优惠营销活动<{}>的规则<{}> 手续费减免活动单日统计表 数据查询失败!", globalInstId, mcNo, CommonConstant.ZERO_COMMON_CONSTANT);
            tmStatMfdDay = new TMStatMfdDay();
            tmStatMfdDay.setTotalQtyAcc(CommonConstant.ZERO_COMMON_CONSTANT);
            tmStatMfdDay.setTotalAmtAcc(CommonConstant.ZERO_COMMON_CONSTANT);
            tmStatMfdDay.setTxnQtyAcc(CommonConstant.ZERO_COMMON_CONSTANT);
            tmStatMfdDay.setTxnAmtAcc(CommonConstant.ZERO_COMMON_CONSTANT);
        }
        //批量处理的日期小于昨天的日期，则需要取之前的数据
        while (DateUtil.compareDate(batchDate, lastDate) <= 0) {
            //循环获取单日统计表数据并累加
            /* 查询商户手续费优惠营销活动单日统计表数据 规则为0的数据*/
            TMStatMfdDay tmStatMfdDay1 = selectMchntFeeDiscountDayStatisticsByPK(globalInstId, mcNo, CommonConstant.ZERO_COMMON_CONSTANT, batchDate);
            if (null == tmStatMfdDay1) {
                rglog.error("机构<{}>的商户手续费优惠营销活动<{}>的规则<{}> 手续费减免活动单日统计表 数据查询失败!", globalInstId, mcNo, CommonConstant.ZERO_COMMON_CONSTANT);
                tmStatMfdDay1 = new TMStatMfdDay();
                tmStatMfdDay1.setTotalQtyAcc(CommonConstant.ZERO_COMMON_CONSTANT);
                tmStatMfdDay1.setTotalAmtAcc(CommonConstant.ZERO_COMMON_CONSTANT);
                tmStatMfdDay1.setTxnQtyAcc(CommonConstant.ZERO_COMMON_CONSTANT);
                tmStatMfdDay1.setTxnAmtAcc(CommonConstant.ZERO_COMMON_CONSTANT);
            }
            copyStatMfdData(tmStatMfd, tmStatMfdDay1);
            //将批次日期加一天继续判断
            batchDate = DateUtil.getBaseDateOffsetDay(batchDate, 1);
        }


        //总消耗额度为周期统计表的值（已包含未跑批昨天的）+ 单日统计值
        String alredyUse = new BigDecimal(tmStatMfd.getTotalAmtAcc()).add(new BigDecimal(tmStatMfdDay.getTotalAmtAcc())).setScale(CommonConstant.DECIMAL_SCALE_TWO, RoundingMode.HALF_UP).toString();

        return alredyUse;
    }

    /**
     * 使用主键取手续费减免活动统计表单条数据
     *
     * @param instId    法人机构编号
     * @param mcNo      营销活动编号
     * @param ruleIndex 规则顺序号
     * @return cc.rengu.igas.momp.common.entity.TMStatMfd 手续费减免活动统计表
     * @throws Exception 异常信息
     * @author liujinan
     * @since 2020/9/16 16:38
     */
    private TMStatMfd selectMchntFeeDiscountStatisticsByPK(String instId, String mcNo, String ruleIndex) throws Exception {

        TMStatMfdMapper tmStatMfdMapper = new TMStatMfdMapperImpl();
        return tmStatMfdMapper.selectMchntFeeDiscountStatisticsByPK(instId, mcNo, ruleIndex);
    }

    /**
     * 使用主键取手续费减免活动单日统计表单条数据
     *
     * @param instId    法人机构号
     * @param mcNo      营销活动编号
     * @param ruleIndex 规则顺序号
     * @param transDate 交易日期
     * @return cc.rengu.igas.momp.common.entity.TMStatMfdDay 手续费减免活动单日统计表
     * @throws Exception 异常信息
     * @author liujinan
     * @since 2020/9/16 16:38
     */
    private TMStatMfdDay selectMchntFeeDiscountDayStatisticsByPK(String instId, String mcNo, String ruleIndex, String transDate) throws Exception {

        TMStatMfdDayMapper tmStatMfdDayMapper = new TMStatMfdDayMapperImpl();
        return tmStatMfdDayMapper.selectMchntFeeDiscountDayStatisticsByPK(instId, mcNo, ruleIndex, transDate);
    }

    /**
     * 使用主键查询统计信息备份表最新一条数据
     *
     * @param instId
     * @param statType
     * @param statPrimaryKey
     * @return cc.rengu.igas.momp.common.entity.TMStatMfdPeriod
     * @throws Exception
     * @author liujinan
     * @date 2021/7/9
     */
    private TMStatBak selectTMStatBakByStatPrimaryKey(String instId, String statType, String statPrimaryKey) throws Exception {

        TMStatBakMapper tmStatBakMapper = new TMStatBakMapperImpl();
        return tmStatBakMapper.selectTMStatBakByStatPrimaryKey(instId, statType, statPrimaryKey);
    }

    /**
     * 手续费减免活动统计数据赋值
     *
     * @param tmStatMfd    手续费减免活动统计表
     * @param tmStatMfdDay 手续费减免活动单日统计表
     * @author Jinan Liu
     * @since 2020/6/5 11:23
     */
    private void copyStatMfdData(TMStatMfd tmStatMfd, TMStatMfdDay tmStatMfdDay) {

        /* 营销活动单日累计-总预算累计消耗笔数 */
        tmStatMfd.setTotalQtyAcc(new BigDecimal(tmStatMfd.getTotalQtyAcc()).add(new BigDecimal(tmStatMfdDay.getTotalQtyAcc())).toString());
        /* 营销活动单日累计-总预算累计消耗金额 */
        tmStatMfd.setTotalAmtAcc(new BigDecimal(tmStatMfd.getTotalAmtAcc()).add(new BigDecimal(tmStatMfdDay.getTotalAmtAcc())).setScale(CommonConstant.DECIMAL_SCALE_TWO, RoundingMode.HALF_UP).toString());
        /* 营销活动单日累计-总预算累计消耗笔数 */
        tmStatMfd.setTxnQtyAcc(new BigDecimal(tmStatMfd.getTxnQtyAcc()).add(new BigDecimal(tmStatMfdDay.getTxnQtyAcc())).toString());
        /* 营销活动单日累计-总预算累计消耗金额 */
        tmStatMfd.setTxnAmtAcc(new BigDecimal(tmStatMfd.getTxnAmtAcc()).add(new BigDecimal(tmStatMfdDay.getTxnAmtAcc())).setScale(CommonConstant.DECIMAL_SCALE_TWO, RoundingMode.HALF_UP).toString());
    }

}
